package com.beiding.hotcode;

import java.util.List;
import java.util.Map;

public abstract class InstanceLifecycle {

    static final InstanceLifecycle EMPTY = new InstanceLifecycle() {
        @Override
        protected void onAfterCreate(MyInstance realInstance) {

        }

        @Override
        protected void onBeforeAbandon(MyInstance realInstance) {

        }

        @Override
        void beforeAbandon(Object instance, Map<String, Map<Integer, List<MethodHolder>>> methods) {
        }

        @Override
        void afterCreate(Object instance, Map<String, Map<Integer, List<MethodHolder>>> methods) {
        }

    };

    //创建前
    void afterCreate(Object instance, Map<String, Map<Integer, List<MethodHolder>>> methods) {
        if (instance != null) {
            RealInstance realInstance = new RealInstance();
            realInstance.instance = instance;
            realInstance.methods = methods;
            try {
                onAfterCreate(realInstance);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    protected abstract void onAfterCreate(MyInstance realInstance);

    void beforeAbandon(Object instance, Map<String, Map<Integer, List<MethodHolder>>> methods) {
        if (instance != null) {
            RealInstance realInstance = new RealInstance();
            realInstance.instance = instance;
            realInstance.methods = methods;
            try {
                onBeforeAbandon(realInstance);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    protected abstract void onBeforeAbandon(MyInstance realInstance);

}
